//
//  StatusOriginalView.swift
//  Sina
//
//  Created by 王杰 on 15/11/17.
//  Copyright © 2015年 王杰. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class StatusOriginalView: UIView {

    var bottomConstraints: Constraint?
    
    //数据模型
    var status: Status? {
        didSet {
            iconImage.sd_setImageWithURL(status?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = status?.user?.name
            memberIconView.image = status?.user?.mbrankImage
            verifiedIconView.image = status?.user?.verified_type_Image
            //时间显示
            timeLabel.text = NSDate.sinaDate(status?.created_at ?? "")?.fullDescription()
            //微博来源
            sourceLabel.text = status?.weiboSource?.linhref().clienName
            
            contentLabel.attributedText = EmoticonManager.emoticonManager.textToImageText(status?.text ?? "", font: contentLabel.font)
            //SnapKit中 表示 移除某个约束条件
            self.bottomConstraints?.uninstall()
            //给配图视图设置数据源
            if let picurls = status?.imageURLs where picurls.count > 0 {
                //有配图视图
                pictureView.imageURLs = status?.imageURLs
                //显示配图视图
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //将约束条件转换成 约束类型
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }

    //设置frame
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置UI
    
    private func setupUI() {
    sepView.backgroundColor = UIColor(white: 0.92, alpha: 1.0)
        addSubview(sepView)
        addSubview(iconImage)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(verifiedIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }

        iconImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
            make.height.equalTo(StatusCellIconWitdth)
            make.width.equalTo(StatusCellIconWitdth)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_top)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
        }
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        
        verifiedIconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconImage.snp_right)
            make.centerY.equalTo(iconImage.snp_bottom)
            
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(iconImage.snp_bottom)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconImage.snp_left)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconImage.snp_left)
            
        }
        self.snp_makeConstraints { (make) -> Void in
            
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    
    }
    //懒加载控件
    private lazy var sepView: UIView = UIView()
    private lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel: UILabel = UILabel(title: "无跌男", size: 14, color: UIColor.greenColor())
    
    private lazy var verifiedIconView :UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))

    private lazy var memberIconView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    
    private lazy var timeLabel: UILabel = UILabel(title: "11:11", size: 14, color: UIColor.darkGrayColor())
    private lazy var sourceLabel: UILabel = UILabel(title: "新浪微博", size: 14, color: UIColor.darkGrayColor())
    private lazy var contentLabel: UILabel = UILabel(title: " 初暖乍寒的季节，清风没有冬天的凛冽，阳光没有六月的炎热，暖中带着一丝凉意，寒中透着舒适的暖，春光泄漏在千山万水中，催红了樱桃，催绿了芭蕉，让寒谷回春，到处姹紫嫣红，草长莺飞，一片春意盎然的勃勃生机。用一种感恩的心情，感谢大自然的馈赠，看远山如黛，春深似海。坐在柔软的草坪上，享受着暖暖的阳光，还有花香中夹杂着泥土的清新气息，仰起头，是蓝蓝的天，白白的云，柔柔的风", size: 14, color: UIColor.darkGrayColor(),margin: StatusCellMargin)
    private lazy var pictureView : StatusPictureView = StatusPictureView()
}




